package controller

import (
	"github.com/gin-gonic/gin"
	"guliedu/model"
	"net/http"
	"strconv"
)

func InsertOneSubject(ctx *gin.Context) {
	title := ctx.Param("title")
	subject, _ := eduSubjectService.SelectSubjectByTitle(title)
	if subject.ID != 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code": 20000,
			"msg":  "分类已存在",
		})
		return
	}

	s := &model.EduSubject{
		Title:    title,
		ParentID: 1,
		Sort:     0,
	}

	eduSubjectService.Insert(s)
	ctx.JSON(http.StatusOK, gin.H{
		"code": 20000,
	})
	return
}

func InsertTwoSubject(ctx *gin.Context) {
	id := ctx.Param("id")
	title := ctx.Param("title")
	subject, _ := eduSubjectService.SelectSubjectByTitle(title)
	if subject.ID != 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code": 20000,
			"msg":  "分类已存在",
		})
		return
	}

	convID, _ := strconv.Atoi(id)
	s := &model.EduSubject{
		Title:    title,
		ParentID: convID,
		Sort:     0,
	}

	eduSubjectService.Insert(s)
	ctx.JSON(http.StatusOK, gin.H{
		"code": 20000,
	})
	return
}

func SelectOneSubject(ctx *gin.Context) {
	subject, _ := eduSubjectService.SelectOneSubject()
	ctx.JSON(http.StatusOK, gin.H{
		"code": 20000,
		"data": subject,
	})
	return
}

func SelectAllSubject(ctx *gin.Context) {
	subjects, _ := eduSubjectService.SelectAllSubjectByOne()
	ctx.JSON(http.StatusOK, gin.H{
		"code": 20000,
		"data": subjects,
	})
	return
}

func FindAllSubject(ctx *gin.Context) {
	subjects, _ := eduSubjectService.Select()
	ctx.JSON(http.StatusOK, gin.H{
		"code": 20000,
		"data": subjects,
	})
	return
}
